package com.common.util.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Proxy;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @文件名称：NetworkUtils.java
 * @文件作者：ljb
 * @文件描述：
 * @版本： 1.0.0
 * @创建时间：2017-12-20 16:46
 * @修改历史：2017-12-20 16:46
 */

public class NetworkUtils {
    public static final String NETWORK_TYPE_WIFI = "wifi";
    public static final String NETWORK_TYPE_MOBILE = "MOBILE";
    public static final String NETWORK_TYPE_3G = "eg";
    public static final String NETWORK_TYPE_2G = "2g";
    public static final String NETWORK_TYPE_WAP = "wap";
    public static final String NETWORK_TYPE_UNKNOWN = "unknown";
    public static final String NETWORK_TYPE_DISCONNECT = "disconnect";

    public static String getIpAddress(Context context) {
        String type = getNetworkTypeName(context);
        return NETWORK_TYPE_WIFI.equals(type)?getWifiIpAddress(context):(!NETWORK_TYPE_DISCONNECT.equals(type) && !NETWORK_TYPE_UNKNOWN.equals(type)?getLocalIpAddress():"");
    }

    public static String getNetworkTypeName(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        String netWorkType = NETWORK_TYPE_DISCONNECT;
        NetworkInfo networkInfo;
        if(cm != null && (networkInfo = cm.getActiveNetworkInfo()) != null) {
            if(networkInfo.isConnected()) {
                String typeName = networkInfo.getTypeName();
                if(NETWORK_TYPE_WIFI.equalsIgnoreCase(typeName)) {
                    netWorkType = NETWORK_TYPE_WIFI;
                } else if(NETWORK_TYPE_MOBILE.equalsIgnoreCase(typeName)) {
                    String host = Proxy.getDefaultHost();
                    netWorkType = TextUtils.isEmpty(host)?(network(context)?NETWORK_TYPE_3G:NETWORK_TYPE_2G):NETWORK_TYPE_WAP;
                } else {
                    netWorkType = NETWORK_TYPE_UNKNOWN;
                }
            }

            return netWorkType;
        } else {
            return netWorkType;
        }
    }

    public static String getWifiIpAddress(Context context) {
        WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wi = wm.getConnectionInfo();
        return network(wi.getIpAddress());
    }

    private static String network(int ip) {
        return (ip & 255) + "." + (ip >> 8 & 255) + "." + (ip >> 16 & 255) + "." + (ip >> 24 & 255);
    }

    private static boolean network(Context context) {
        TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        if(tm == null) {
            return false;
        } else {
            switch(tm.getNetworkType()) {
                case 0:
                    return false;
                case 1:
                    return false;
                case 2:
                    return false;
                case 3:
                    return true;
                case 4:
                    return false;
                case 5:
                    return true;
                case 6:
                    return true;
                case 7:
                    return false;
                case 8:
                    return true;
                case 9:
                    return true;
                case 10:
                    return true;
                case 11:
                    return false;
                case 12:
                    return true;
                case 13:
                    return true;
                case 14:
                    return true;
                case 15:
                    return true;
                default:
                    return false;
            }
        }
    }

    public static String getLocalIpAddress() {
        try {
            Enumeration enumeration = NetworkInterface.getNetworkInterfaces();

            while(enumeration.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface)enumeration.nextElement();
                Enumeration enumeration1 = networkInterface.getInetAddresses();

                while(enumeration1.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress)enumeration1.nextElement();
                    if(!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException se) {
            se.printStackTrace();
        }

        return "";
    }

    public static boolean isNetworkAvailable(Context context) {
        if(context == null) {
            return false;
        } else {
            ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if(cm != null) {
                NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
                if(networkInfos != null) {
                    for(int i = 0; i < networkInfos.length; i++) {
                        if(networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }

    public static boolean isWifi(Context context) {
        if(context == null) {
            return false;
        } else {
            ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
        }
    }
}
